package com.zy.pro.city.web;

import java.io.PrintWriter;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zy.core.model.PageView;
import com.zy.core.web.BaseController;
import com.zy.pro.city.model.City;
import com.zy.pro.city.service.CityService;
import com.zy.pro.city.web.form.CitySearchForm;
/**
 * 城市管理控制器
 *
 */
@Controller
@RequestMapping("/control/city/cityController")
public class CityController extends BaseController {

	private static final Logger logger = Logger.getLogger(CityController.class);
	
	/**
	 * 城市管理服务类
	 */
	@Resource(name = "cityServiceImpl")
	private CityService cityService;
	
	/**
	 * 系统-列出城市管理
	 */
	@RequestMapping("/sysCityList")
	public String sysCityList(CitySearchForm form, ModelMap map) {
		try {
			//设置查询排序
			//TODO eg:form.getOrderby().put("id", "asc");
			if(form.getCityName() != null && form.getCityName() != ""){
				form.setCityName(form.getCityName().trim());
			}
			
			form.getOrderby().put("cityCreateTime", "desc");
			
			//创建页面对象
			PageView<City> pageView = new PageView<City>(12, form.getPage());
			form.setFirstResult(pageView.getFirstResult());
			form.setMaxResult(pageView.getMaxresult());
			pageView.setQueryResult(cityService.getData(form));
			map.addAttribute("pageView", pageView);
			map.addAttribute("form", form);
			map.addAttribute("reqUrl", "/control/city/cityController/sysCityList.do");
			//TODO 转发页面
			return "control/system/city/sysCityList";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 系统-编辑城市状态
	 */
	@RequestMapping("/editStatus")
	public void editStatus(CitySearchForm form, ModelMap map,HttpServletRequest request,HttpServletResponse response) {
		try {
			response.setContentType("text/html");
			PrintWriter out;
			out = response.getWriter();
			City s = cityService.find(form.getCityId());
			if(null!=s){
				City city = new City();
				city.setCityId(form.getCityId());
				city.setCityStatus(form.getCityStatus());//1:正常,2:冻结,3:删除
				cityService.updateBySelective(city);
				out.print("true");
			}else{
				out.print("false");
			}
		} catch (Exception e) {
			logger.error(e);
		}
	}
	
	/**
	 * 系统-显示添加城市管理
	 */
	@RequestMapping("/dispalySysAdd")
	public String dispalySysAdd(City city, ModelMap map) {
		try {
			//TODO 加载显示页面数据
			
			map.addAttribute("operateStatus", "sysAdd");
			map.addAttribute("city", city);
			//TODO 转发页面
			return "/control/system/city/sysEditCity";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 系统-显示编辑城市管理
	 */
	@RequestMapping("/dispalySysEdit")
	public String dispalySysEdit(City city, ModelMap map) {
		try {
			//TODO 加载显示页面数据
			city = cityService.find(city.getCityId());
			map.addAttribute("operateStatus", "sysEdit");
			map.addAttribute("city", city);
			//TODO 转发页面
			return "/control/system/city/sysEditCity";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	

	/**
	 * 系统-添加城市管理
	 */
	@RequestMapping("/sysAdd")
	public String sysAdd(City city, ModelMap map) {
		try {
			//TODO 设置shop  eg:主键信息
			city.setCityStatus(1);
			city.setCityCreateTime(new Date());
			cityService.save(city);
			return sysCityList(new CitySearchForm(), map);
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 系统-编辑城市管理
	 */
	@RequestMapping("/sysEdit")
	public String sysEdit(City city,ModelMap map, HttpServletRequest request) {
		try {
			//TODO:处理店铺管理
			
			cityService.updateBySelective(city);
			return sysCityList(new CitySearchForm(), map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}
	
	
	
	/**
	 * 列出城市管理
	 */
	@RequestMapping("/list")
	public String list(CitySearchForm form, ModelMap map) {
		try {
			//设置查询排序
			//TODO eg:form.getOrderby().put("id", "asc");
			
			
			//创建页面对象
			PageView<City> pageView = new PageView<City>(12, form.getPage());
			form.setFirstResult(pageView.getFirstResult());
			form.setMaxResult(pageView.getMaxresult());
			pageView.setQueryResult(cityService.getData(form));
			map.addAttribute("pageView", pageView);
			map.addAttribute("form", form);
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 显示添加城市管理
	 */
	@RequestMapping("/displayAdd")
	public String displayAdd(City city, ModelMap map) {
		try {
			//TODO 加载显示页面数据
			
			
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 添加城市管理
	 */
	@RequestMapping("/add")
	public String add(City city, ModelMap map) {
		try {
			//TODO 设置city  eg:主键信息
			
			cityService.save(city);
			return list(new CitySearchForm(), map);
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 显示编辑城市管理
	 */
	@RequestMapping("/displayEdit")
	public String displayEdit(CitySearchForm form, ModelMap map) {
		try {
			//TODO 查询对象
			//eg:City city = cityService.find(form.getId());
			//eg:map.addAttribute("entity", city);
			
			
			//TODO 转发页面
			return "";
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 编辑城市管理
	 */
	@RequestMapping("/edit")
	public String edit(City City, ModelMap map) {
		try {
			//TODO:处理城市管理
			
			
			cityService.updateBySelective(City);
			return list(new CitySearchForm(), map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}

	/**
	 * 删除单个城市管理
	 */
	@RequestMapping("/deletecity")
	public String delete(CitySearchForm form, ModelMap map) {
		try {
			//TODO  eg:cityService.delete(form.getId());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 批量删除城市管理
	 */
	@RequestMapping("/batchDelete")
	public String deletes(CitySearchForm form, ModelMap map) {
		try {
//			TODO  eg:cityService.batchDelete(form.getIds());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
}
